package tables

import (
	"strconv"

	"github.com/GoAdminGroup/go-admin/context"
	"github.com/GoAdminGroup/go-admin/modules/db"
	"github.com/GoAdminGroup/go-admin/plugins/admin/modules/table"
	"github.com/GoAdminGroup/go-admin/template/types"
	"github.com/GoAdminGroup/go-admin/template/types/form"
)

func GetSeedbedAllocationBatchRecordTable(ctx *context.Context) table.Table {

	SeedbedAllocationBatchRecord := table.NewDefaultTable(table.DefaultConfigWithDriver("mysql"))

	info := SeedbedAllocationBatchRecord.GetInfo()
	info.SetFilterFormLayout(form.LayoutTwoCol)
	info.AddField("Id", "id", db.Int).
		FieldFilterable().
		FieldSortable()
	info.AddField("订单", "name", db.Varchar).
		FieldJoin(types.Join{
			Table:     "orders",
			JoinField: "id",
			Field:     "order_id",
		}).FieldFilterable(types.FilterType{FormType: form.SelectSingle}).
		FieldFilterOptionsFromTable("orders", "name", "name", func(model *db.SQL) *db.SQL {
			return model.Where("id", ">", 0)
		}).
		FieldSortable()
	info.AddField("大棚", "name", db.Varchar).
		FieldJoin(types.Join{
			Table:     "greenhouse",
			JoinField: "id",
			Field:     "greenhouse_id",
		}).
		FieldFilterable(types.FilterType{FormType: form.SelectSingle}).
		FieldFilterOptionsFromTable("greenhouse", "name", "name", func(model *db.SQL) *db.SQL {
			return model.Where("id", ">", 0).Where("is_active", "=", 1)
		}).
		FieldDisplay(func(value types.FieldModel) interface{} {
			return value.Value
		}).
		FieldSortable()
	info.AddField("大棚分配记录", "greenhouse_allocation_id", db.Int).
		FieldFilterable(types.FilterType{FormType: form.SelectSingle}).
		FieldFilterOptions(getGreenhouseAllocationOptions()).
		FieldDisplay(func(value types.FieldModel) interface{} {
			return getGreenhouseAllocationNameById(strconv.FormatInt(value.Row["greenhouse_allocation_id"].(int64), 10))
		}).
		FieldEditAble()
	info.AddField("苗床列表", "seedbed_ids", db.Varchar).
		FieldFilterable().
		FieldSortable().
		FieldEditAble()
	info.AddField("分配数量", "quantity", db.Int).
		FieldFilterable().
		FieldSortable().
		FieldEditAble()
	info.AddField("分配盘数", "tray_number", db.Int).
		FieldFilterable().
		FieldSortable().
		FieldEditAble()
	info.AddField("每盘数量", "unit_number", db.Int).
		FieldFilterable().
		FieldSortable().
		FieldEditAble()
	info.AddField("备注", "remark", db.Varchar).
		FieldFilterable().
		FieldSortable().
		FieldEditAble()
	info.AddField("创建时间", "created_at", db.Timestamp).
		FieldSortable()
	info.AddField("更新时间", "updated_at", db.Timestamp).
		FieldSortable()

	info.SetTable("seedbed_allocation_batch_record").SetTitle("苗床分配记录").SetDescription("SeedbedAllocationBatchRecord")

	formList := SeedbedAllocationBatchRecord.GetForm()
	formList.AddField("Id", "id", db.Int, form.Default).
		FieldDisableWhenCreate().
		FieldDisableWhenUpdate()
	formList.AddField("订单", "order_id", db.Int, form.SelectSingle).FieldOptionsFromTable("orders", "name", "id", func(model *db.SQL) *db.SQL {
		return model.Where("id", ">", 0)
	}).FieldHelpMsg("请选择订单")
	formList.AddField("大棚", "greenhouse_id", db.Int, form.SelectSingle).FieldOptionsFromTable("greenhouse", "name", "id", func(model *db.SQL) *db.SQL {
		return model.Where("id", ">", 0).Where("is_active", "=", 1)
	}).FieldHelpMsg("请选择大棚")
	formList.AddField("大棚分配记录", "greenhouse_allocation_id", db.Int, form.SelectSingle).FieldOptions(getGreenhouseAllocationOptions())
	formList.AddField("苗床列表", "seedbed_id", db.Varchar, form.Text)
	formList.AddField("苗床", "seedbed_id", db.Int, form.SelectSingle).FieldOptions(getSeedbedOptions()).FieldHelpMsg("请选择苗床")
	formList.AddField("分配数量", "quantity", db.Int, form.Number)
	formList.AddField("分配盘数", "tray_number", db.Int, form.Number)
	formList.AddField("每盘数量", "unit_number", db.Int, form.Number)
	formList.AddField("备注", "remark", db.Varchar, form.Text)
	formList.AddField("创建时间", "created_at", db.Timestamp, form.Datetime).
		FieldDisableWhenUpdate().
		FieldHide().FieldNowWhenInsert()
	formList.AddField("修改时间", "updated_at", db.Timestamp, form.Datetime).
		FieldDisableWhenCreate().
		FieldHide().FieldNowWhenUpdate()

	formList.SetTable("seedbed_allocation_batch_record").SetTitle("苗床分配记录").SetDescription("SeedbedAllocationBatchRecord")

	return SeedbedAllocationBatchRecord
}
